package com.cycplus.test.base

import android.content.pm.PackageManager
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.viewbinding.ViewBinding
import com.cycplus.test.R
import com.cycplus.test.extensions.inflateBindingWithGeneric
import com.cycplus.test.utils.ScreenUtil
import com.gyf.immersionbar.ImmersionBar
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import io.reactivex.functions.Consumer

abstract class BaseBindingActivity<VB : ViewBinding> : RxAppCompatActivity() {

    protected val mBinding: VB by lazy { inflateBindingWithGeneric(layoutInflater) }
    private var rootView: View? = null

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (adaptScreen()) {
            ScreenUtil.adapterScreen(this, 390, false)
        }
        rootView = mBinding.root
        setContentView(rootView)
        initImmersionBar()
        initView(rootView!!, savedInstanceState)
        initViewModel()
    }

    open fun adaptScreen(): Boolean = true

    open fun initImmersionBar() {
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.colorPrimary).navigationBarColor(R.color.colorPrimary)
            .autoDarkModeEnable(true).init()
    }

    protected abstract fun initView(rootView: View, savedInstanceState: Bundle?)

    open fun initViewModel(){

    }

    protected fun checkSelfPermission(
        permissions: List<String>,
        grantedCallback: Consumer<List<String>>,
        deniedCallback: Consumer<List<String>>
    ) {
        var result = true
        val grantedList = arrayListOf<String>()
        val deniedList = arrayListOf<String>()
        permissions.forEach {
            if (ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED) {
                result = false
                deniedList.add(it)
            } else {
                grantedList.add(it)
            }
        }
        if (result) {
            grantedCallback.accept(grantedList)
        } else {
            deniedCallback.accept(deniedList)
        }
    }
}